package com.example.mapper;

import com.example.entity.Question;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface QuestionMapper {

    /**
     * 查询所有问题
     *
     * @return 问题列表
     */
    @Select("SELECT * FROM question")
    List<Question> selectAllQuestions();

    // 概览统计
    @Select("SELECT COUNT(*) FROM question")
    int getTotalQuestions();

    @Select("SELECT COUNT(*) FROM question WHERE is_solve = '1'")
    int getSolvedQuestions();

    @Select("SELECT COUNT(*) FROM question WHERE is_solve = '2'")
    int getPendingQuestions();

    @Select("SELECT COUNT(*) FROM question WHERE important = '3'")
    int getProcessingQuestions();

    // 分类统计
    @Select("SELECT COUNT(*) FROM question WHERE type = #{type}")
    int getCategoryCount(@Param("type") String type);

    // 时间趋势
    @Select("SELECT DATE_FORMAT(time, '%Y-%m') AS month, COUNT(*) AS count " +
            "FROM question " +
            "GROUP BY DATE_FORMAT(time, '%Y-%m') " +
            "ORDER BY DATE_FORMAT(time, '%Y-%m')")
    List<Map<String, Object>> getQuestionTrend();

    /**
     * 插入问题
     *
     * @param question 提交的问题数据
     * @return 插入结果，返回影响的行数
     */
    @Insert("INSERT INTO question (user_id, is_solve, important, description, time, type, title) " +
            "VALUES (#{userId}, #{isSolve}, #{important}, #{description}, #{time}, #{type}, #{title})")
    int insertQuestion(Question question);

}
